<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>抽奖条件</title>
    <link rel="stylesheet" href="./css/index.css" />
    <style>
      * {
        box-sizing: border-box;
        margin: 0;
        padding: 0;
        vertical-align: middle;
      }
      body {
        position: relative;
        padding: 10px;
        font-size: 16px;
      }
      .title {
        /* width: 100%; */
        height: 30px;
        /* line-height: 30px; */
        /* background-color: ; */
        text-align: center;
      }
      .title > img {
        height: 100%;
      }
      .title h3 {
        display: inline-block;
        text-align: center;
        line-height: 30px;
      }
      .container {
        position: absolute;
        top: 50px;
        left: 0;
        width: 100%;
        height: 100%;
      }
      .item {
        width: 100%;
        height: 50px;
        line-height: 50px;
        padding-left: 25px;
        box-sizing: border-box;
      }
      /* .item::after {
        content: "";
        display: block;
        width: 80%;
        box-sizing: border-box;
        border: 1px solid #ccc;
      } */
      .item select,
      .item input {
        width: 180px;
        height: 30px;
        line-height: 30px;
        outline: none;
        box-sizing: border-box;
      }
      .btn {
        width: 100%;
        height: 30px;
        margin-top: 10px;
        line-height: 30px;
        background-color: green;
        border: none;
        outline: none;
        color: #fff;
        border-radius: 5px;
      }
      .alertBox {
        position: absolute;
        width: 100px;
        height: 30px;
        line-height: 30px;
        top: 60px;
        left: 50%;
        transform: translateX(-50%);
        background: #cff0cf;
        text-align: center;
        border-radius: 5px;
      }
      .success {
        color: green;
        border: 1px solid #b5d0b5;
      }
      .warning {
        color: green;
        border: 1px solid #b5d0b5;
      }
      textarea {
        width: 100%;
        margin-top: 80vh;
      }
    </style>
    <script>
      if (!localStorage.getItem("token")) {
        window.location.href = "./index.html";
      }
    </script>
  </head>
  <body>
    <div class="title mb8">
      <div class="icon iconfont backBtn icon-fanhui"></div>
      <img src="http://www.aknien.net/static/img/newLogo.312db06b.png" alt="" />
      <h3>抽奖条件</h3>
    </div>
    <div class="container">
      <div class="item">
        <label>抽奖种类：</label>
        <select id="type">
          <option value="">请选择</option>
          <option value="0">特等奖</option>
          <option value="1">一等奖</option>
          <option value="2">二等奖</option>
          <option value="3">三等奖</option>
          <option value="4">小幸运</option>
        </select>
      </div>
      <div class="item">
        <label>本轮中奖人数：</label><input id="sum" type="text" />
      </div>
      <div class="item">
        <label>每组中奖人数：</label><input id="once" type="text" />
      </div>
      <button class="btn" id="submit">设置</button>
      <button class="btn" id="clear">清空</button>
      <div>
        <textarea id="list"></textarea>
      </div>
    </div>
    <script src="./js/staitcFiled.js"></script>
    <script src="./js/back.js"></script>

    <script>
      var submit = document.querySelector("#submit");
      var clear = document.querySelector("#clear");
      clear.addEventListener("click", function () {
        document.querySelector("#type").value = "";
        document.querySelector("#sum").value = "";
        document.querySelector("#once").value = "";
        document.querySelector("#list").value = "";
      });
      submit.addEventListener("click", function () {
        var type = document.querySelector("#type").value;
        var sum = document.querySelector("#sum").value;
        var once = document.querySelector("#once").value;
        var list = document.querySelector("#list").value;
        if (list) {
          list = newList(list);
          fetch(`http://${baseUrl}:3000/user/setList`, {
            method: "POST",
            headers: {
              "Content-Type": "application/json",
              authorization: "Bearer " + localStorage.getItem("token"),
            },
            body: JSON.stringify({ list }),
          })
            .then((res) => res.json())
            .then((res) => {
              console.log(res);
            })
            .catch((err) => {
              console.log(err);
            });
        }
        if (!(type || sum || once)) {
          throw new Error("请设置抽奖条件");
        } else if (Number(once) > Number(sum)) {
          throw new Error("每组中奖人数不得大于本轮抽奖人数");
        }
        switch (type) {
          case "0":
            type = "特等奖";
            break;
          case "1":
            type = "一等奖";
            break;
          case "2":
            type = "二等奖";
            break;
          case "3":
            type = "三等奖";
            break;
          case "4":
            type = "小幸运";
            break;
        }
        const data = {
          type,
          sum: Number(sum),
          once: Number(once),
          list,
        };
        fetch(`http://${baseUrl}:3000/user/setting`, {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
            authorization: "Bearer " + localStorage.getItem("token"),
          },
          body: JSON.stringify(data),
        })
          .then((res) => res.json())
          .then((res) => {
            console.log(res);
            let alert = document.createElement("div");
            alert.classList.add("alertBox");

            if (res.code === 200) {
              alert.classList.add("success");
            } else {
              alert.classList.add("warning");
            }

            alert.innerHTML = res.msg;
            document.body.appendChild(alert);
            setTimeout(function () {
              document.body.removeChild(alert);
            }, 3000);
          })
          .catch((err) => {
            console.log(err);
          });
      });
      function newList(list) {
        let regx = /[\s|\||\r]/g;
        list = list.replace(/[\r|\n|\r\n|,|，]/g, " ");
        list = list.trim();
        list.replace(/\s/g, "");
        list = list.split(regx);
        let newList = [];
        list = list.forEach((el) => {
          if (el) {
            newList.push(el);
          }
        });
        newList = newList.join(",");
        return newList;
      }
    </script>
  </body>
</html>
